package businessEntities;

final public class NodeData {

    final private String previousFilters;
    final private String currentFilter;
    final private String dataBaseName;
    final private String tableName;

    /**
     * Construct new NodeData according to it's parent.
     * @param currentFilter
     * @param parent
     * @throws IllegalArgumentException if <code>currentFilter == null || currentFilter.equals("")</code>
     * @throws NullPointerException if <code>parent == null</code>
     */
    public NodeData(String currentFilter, NodeData parent) {
        if (currentFilter == null || currentFilter.equals("")) {
            throw new IllegalArgumentException
                    ("Illegal 'current filter' string was passed to to the constructor ['" 
                    + currentFilter + "']");
        }
        if (parent == null) {
            throw new IllegalArgumentException("null 'parent' NodeData was passed to the constructor");
        }

        this.previousFilters = parent.getWhereSentence();
        this.currentFilter = currentFilter;
        this.dataBaseName = parent.dataBaseName;
        this.tableName = parent.tableName;
    }

    //Private constructor for root node
    private NodeData(String dataBaseName, String tableName) {
        this.previousFilters = "";
        this.currentFilter = "";
        this.dataBaseName = dataBaseName;
        this.tableName = tableName;
    }

    /**
     * Factory for creating NodeData that has no parent.
     * @param dataBase
     * @param tableName
     * @return 
     */
    public static NodeData createRootNodeData(String dataBase, String tableName) {
        return new NodeData(dataBase, tableName);
    }

    public String getDataBaseName() {
        return dataBaseName;
    }

    public String getTableName() {
        return tableName;
    }

    public String getPreviousFilters() {
        return previousFilters;
    }

    public String getCurrentFilter() {
        return currentFilter;
    }

    @Override
    public String toString() {
        if (isRootFilter()) {
            return dataBaseName + "." + tableName;
        } else {
            return currentFilter;
        }
    }

    /**
     * @return String - the where sentence represented by <code>this</code>.
     */
    public String getWhereSentence() {
        if (isRootFilter()) {
            return "";
        } else if (previousFilters.equals("")) {
            return currentFilter;
        } else {
            return "(" + previousFilters + ") AND (" + currentFilter + ")";
        }
    }

    public boolean isRootFilter() {
        return currentFilter.equals("");
    }
}
